<?php

namespace App\GraphQL\Mutations\Auth;

use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use App\Models\User;
use Illuminate\Support\Carbon;
use App\Models\SmsCode;
use App\Notifications\VerificationCode;
class SendSmsCode
{
    /**
     * Return a value for the field.
     *
     * @param  null  $rootValue Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param  mixed[]  $args The arguments that were passed into the field.
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context Arbitrary data that is shared between all fields of a single query.
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     * @return mixed
     */
    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        //查询用户是否存在
        $user = User::where('phone',$args['phone'])->first();
        if(!$user){
            throw new \Exception('用户不存在!');
        }
        //查询2分钟内是否已经发送过
        $oldCode = SmsCode::where('phone',$args['phone'])->where('expired_at','>',Carbon::now()->format('Y-m-d H:i:s'))->first();
        if ($oldCode){
            throw new \Exception('两分钟后在重试!');
        }
        //发送验证码
        $user->notify(new VerificationCode());
        return [
            'status'=> '200',
            'message'=>'发送成功'
        ];
    }
}
